<?php
class masterbarangcontroller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('masterbarang_model');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index()
	{
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('masterbarangcontroller/index');
		$config['total_rows'] = $this->masterbarang_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
		if (isset($_POST['btnview'])) 
		{
			if($_POST['kdtransview'] == "0")
			{         
				$dataprofile = array(
					'page_title' => 'Master > Barang',
					'name' => $this->session->userdata('fullname'),
					'mstrbarang' => $this->masterbarang_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''		
				);		
			}
			else if($_POST['kdbrgview'] == "1")
			{
				$this->masterbarang_model->setKodeBarang($_POST['brgview']);
				$dataprofile = array(
					'page_title' => 'Master > Barang',
					'name' => $this->session->userdata('fullname'),
					'mstrbarang' => $this->masterbarang_model->getkdbaranglike(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''	
				);
			}
			else if($_POST['kdbrgview'] == "2")
			{
				$this->masterbarang_model->setPruductname($_POST['brgview']);
				$dataprofile = array(
					'page_title' => 'Master > Barang',
					'name' => $this->session->userdata('fullname'),
					'mstrbarang' => $this->masterbarang_model->getnamabaranglike(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''			
				);
			}
		}
		else
		{
			$dataprofile = array(
				'page_title' => 'Master > Barang',
				'name' => $this->session->userdata('fullname'),
				'mstrbarang' => $this->masterbarang_model->getListlimit($config['per_page'],$page),
				'links' => $this->pagination_editable->create_links(),
				'message' => ''	
			);	
		}	
		$content = array(
			'content' => 'master/barang/view'
		);
		$this->template->load('templates/default',$content,$dataprofile);	
	}
	
	function tambahmasterbarang() 
	{
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('masterbarangcontroller/index');
		$config['total_rows'] = $this->masterbarang_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
        if (isset($_POST['btnSave'])) 
		{	
			if (isset($_POST['kdbrng']) &&
					isset($_POST['nmbrng']) &&
                    isset($_POST['description']) &&
					isset($_POST['tpunit']) &&
                    !($_POST['kdbrng'] == '') &&
                    !($_POST['nmbrng'] == '') &&
					!($_POST['description'] == '') &&
                    !($_POST['tpunit'] == '')) 
			 {
				/*$query = $this->db->query("select count(KodeBarang) as kdbrg from product_header where KodeBarang='"& $_POST['kdbrng'] &"'");
				$kd = "";
				if($query->num_rows()>0)
				{
					$dataprofile = array(
						'page_title' => 'Master > Barang',
            			'name' => $this->session->userdata('fullname'),
						'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Kode Barang sudah pernah terpakai </div>',
						'mstrbarang' => $this->masterbarang_model->getList());
                	$content = array(
                    	'content' => 'master/barang/view');
                	$this->template->load('template/default', $content, $dataprofile);
				}
				else
				{*/
					$this->masterbarang_model->setKodeBarang($_POST['kdbrng']);
					$this->masterbarang_model->setProductname($_POST['nmbrng']);
					$this->masterbarang_model->setTypeunit($_POST['tpunit']);
					$this->masterbarang_model->setDescription($_POST['description']);
					$this->masterbarang_model->setCreatedBy($this->session->userdata('userid'));
					$tanggal=date('Y-m-d H:i:s', strtotime('now'));
					$this->masterbarang_model->setCreatedOn($tanggal);
					$this->masterbarang_model->setLastUpdateBy($this->session->userdata('userid'));
					$this->masterbarang_model->setLastUpdateOn($tanggal);
					
					$this->masterbarang_model->insert();
					
					redirect('masterbarangcontroller');
				//}
            } 
			else 
			{
                $dataprofile = array(
					'page_title' => 'Master > Barang',
            		'name' => $this->session->userdata('fullname'),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Lengkapi Form Di Bawah Untuk Menambah Master Barang </div>',
					'mstrbarang' => $this->masterbarang_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links()
                );
                $content = array(
                    'content' => 'master/barang/view'
                );
                $this->template->load('templates/default', $content, $dataprofile);
            }
        } else {
           $dataprofile = array(
			'page_title' => 'Master > Barang',
			'name' => $this->session->userdata('fullname'),
			'mstrbarang' => $this->masterbarang_model->getListlimit($config['per_page'],$page),
			'links' => $this->pagination_editable->create_links(),
			'message' =>''		
			);		
			$content = array(
				'content' => 'master/barang/view'
			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }
	
	function ubahmasterbarang()
	{
		$idkddet = $this->uri->segment(3);
		$this->masterbarang_model->setKodeBarang($idkddet);
        if (isset($_POST['btnSave'])) 
		{
			$this->masterbarang_model->setKodeBarang($_POST['kdbrng']);
			$this->masterbarang_model->setProductname($_POST['nmbrng']);
            $this->masterbarang_model->setTypeunit($_POST['tpunit']);
			$this->masterbarang_model->setDescription($_POST['description']);
 			$this->masterbarang_model->setLastUpdateBy($this->session->userdata('userid'));
			$tanggal=date('Y-m-d H:i:s', strtotime('now'));
			$this->masterbarang_model->setLastUpdateOn($tanggal);
			
            $this->masterbarang_model->ubahmasterbarang();

            redirect('masterbarangcontroller');
        } else {
            $this->masterbarang_model->setKodeBarang($idkddet);
            $dataprofile = array(
				'page_title' => 'Master > Barang',
				'name' => $this->session->userdata('fullname'),
				'mstrbarang' => $this->masterbarang_model->getBarangByKode()			
			);		
			$content = array(
				'content' => 'master/barang/edit'
			);
			$this->template->load('templates/default',$content,$dataprofile);
		}
	}
	
	function deletemasterbarang() {
        $kdbrng = $this->uri->segment(3);
        $this->masterbarang_model->setKodeBarang($kdbrng);
        $this->masterbarang_model->deletemasterbarang();
        redirect('masterbarangcontroller');
    }
}
?>